package com.ztgf.shopping.api.cms.shop;

import com.qiyuesuo.sdk.v2.SdkClient;
import com.qiyuesuo.sdk.v2.request.ContractDownloadRequest;
import com.qiyuesuo.sdk.v2.utils.IOUtils;
import com.ztgf.common.constant.Result;
import com.ztgf.order.models.dto.OrderContractRelationDTO;
import com.ztgf.order.models.service.IOrderContractRelationService;
import com.ztgf.shopping.models.service.shop.IShopQiyuesuoInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 契约锁店铺信息控制器
 *
 * @author YUE
 * @Date 2020-06-15 16:18:38
 */

@RestController
@RequestMapping("/cms/shopQiyuesuoInfo")
@Slf4j
@Api(value = "ShopQiyuesuoInfoController",tags = "契约锁店铺信息接口")
public class ShopQiyuesuoInfoController {

    @Value("${shop.qiyuesuo.service.sdkUrl}")
    private String serverUrl;

    @Value("${shop.qiyuesuo.service.appToken}")
    private String accessKey;

    @Value("${shop.qiyuesuo.service.appSecret}")
    private String accessSecret;

    @DubboReference
    private IShopQiyuesuoInfoService shopQiyuesuoInfoService;

    @DubboReference
    private IOrderContractRelationService orderContractRelationService;

    /**
     *查看合同
     * @return
     */
    @ApiOperation(value = "查看合同返回合同url",notes = "查看合同返回合同url")
    @PostMapping("/getContactUrl")
    public Result<String> getContactUrl(Long contractId){
        String contractString = shopQiyuesuoInfoService.getContractString(contractId);
        return new Result<>(contractString);
    }

    /**
     * 根据订单id下载合同
     * @param orderId 订单id
     * @return
     */
    @GetMapping("/orderContractDownload/{orderId}")
    public void orderContractDownload(@PathVariable(value = "orderId") Long orderId, HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition", "attachment;fileName=" + orderId + ".zip");
        OrderContractRelationDTO orderContractRelationDTO = orderContractRelationService.getByOrderId(orderId);
        SdkClient sdkClient = new SdkClient(serverUrl, accessKey, accessSecret);
        // 下载合同
        //Long.parseLong(orderContractRelationDTO.getContractId())
        ContractDownloadRequest request = new ContractDownloadRequest(Long.parseLong(orderContractRelationDTO.getContractId()));
        List<String> str = new ArrayList<>();
        str.add("CONTRACT");
        request.setDownloadItems(str);
        log.info(request.getUrl());
        OutputStream fos = response.getOutputStream();
        //FileOutputStream fos = new FileOutputStream("E:/module/doc.zip");
        sdkClient.download(request, fos);
        IOUtils.safeClose(fos);
        log.info("下载合同成功");
    }

}
